home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Tools / nShell™ 1.0.3 / nShell / User's Guide / 07 Shell Variables < prev    next >
Encoding:
Text File  |  1994-09-04  |  3.3 KB  |  97 lines  |  [TEXT/ttxt]

  1. 07 Shell Variables
  2. ==================
  3.  
  4. Shell variables are used to hold working values within each nShell(tm) document.  There are standard variables which are used by nShell to manage the processes of the shell.  In addition, the user may define custom shell variables.  Such variables provide a method of shorthand, often easing shell use.
  5.  
  6. Standard Variables
  7. ------------------
  8.  
  9. The standard shell variables are listed below:
  10.  
  11. TMP        - The directory used to hold temporary files
  12. PATH    - The command search path
  13. HOME    - The location of the nShell application
  14. PWD     - The current working directory
  15.  
  16. The PATH variable may be modified using the "path"command.  The "cd" command updates the PWD variable.  In addition, the user may override any of these values using the "set" command.
  17.  
  18. By convention, upper case characters are used for shell control variables and lower case characters are used for user variables.
  19.  
  20. The "set", "unset" and "env" commands allow you to access shell variables from the command line.
  21.  
  22. Special Variables
  23. -----------------
  24.  
  25. The following variables serve special purposes within the nShell environment:
  26.  
  27. $?        The return value from the previous command
  28. $#        The number of parameters passed to a script
  29. $0        The name of an executing script
  30. $1..$n        Parameters passed to a script
  31.  
  32. set
  33. ---
  34.  
  35. The "set" command creates or modifies shell variables.  Its format is "set <name> <value>".  Variable names may be up to 31 characters in length and their values may be up to 255 characters.  Consider the following command:
  36.  
  37.     % set kitty "meow meow meow"
  38.  
  39. This sets the value of a variable called "kitty" to "meow meow meow".
  40.  
  41. env
  42. ---
  43.  
  44. The "env" (short for environment) command lists the contents of one or more shell variables:
  45.  
  46.     % env kitty
  47.     kitty="meow meow meow"
  48.  
  49. An "env" command with no parameters lists all current shell variables.
  50.  
  51. unset
  52. -----
  53.  
  54. To remove an existing variable, use the "unset" command.  Consider the sequence:
  55.  
  56.     % set duck quack
  57.     % env duck
  58.     duck="quack"
  59.     % unset duck
  60.     % env duck
  61.     % 
  62.  
  63. Using Shell Variables
  64. ---------------------
  65.  
  66. To evaluate or "expand" a shell variable within a command line, the "$" character is used.  The shell looks up the name immediately following a "$" and substitutes its value:
  67.  
  68.     % set duck quack
  69.     % echo $duck
  70.     quack
  71.  
  72. The variable expansion works within double quotes also:
  73.  
  74.     % echo "Did I hear a $duck?"
  75.     Did I hear a quack?
  76.  
  77. Single quotes may be used when you want to prevent variable expansion:
  78.  
  79.  % echo 'Did I hear a $duck?'
  80.  Did I hear a $duck?
  81.  
  82. Sometimes we get in a bind because we want to put the variable right up against other text.  The following command fails because the shell reads it as asking for a variable called "ducking":
  83.  
  84.  % echo "Did I hear $ducking?"
  85.  Did I hear $ducking?
  86.  
  87. The "{}" characters may be used to isolate a variable name:
  88.  
  89.  % echo "Did I hear ${duck}ing?"
  90.  Did I hear quacking?
  91.  
  92. Variables in Scripts
  93. --------------------
  94.  
  95. Shell variables may be used within scripts to manage passed parameters and track internal conditions.  The "Scripting" section of this manual describes how shell parameters become the default shell variables $0..$n.  In addition, the "set" "unset" and "env" commands may be used to create and modify variables within a script.
  96.  
  97. It is important to note that while a script may modify any variable, all changes are temporary and are discarded when the script terminates.